64

Beginner’s Guide to Code Algorithms

64

STEP 22 Continued (fill emptycells)

      sbox(i, j) =​ 45 -​ whatisthisnumber

      Cells(i, j) =​ sbox(i, j)

      Call updatecantbelist(sbox(i, j), i, j)

    End If

End If

Next j

Next i

‘ now check all columns row by row for all numbers 1-​9 (putnumber)

‘ if the number is invalid in 8 of the nine cells in the same row, then the number

‘ must belong to the ninth cell (empty, of course)

For putnumber =​ 1 To 9

:

For i =​ 1 To 9

    numberfound =​ 0

For j =​ 1 To 9

    If sbox(i, j) =​ putnumber Then numberfound =​ 1

Next j

      If numberfound =​ 0 Then

        notahomecount =​ 0

        For j =​ 1 To 9

          notahome =​ 0

          If sbox(i, j) <> ““ Then notahome =​ 1

          For k =​ 1 To 9

            If cantbelist(i, j, k) =​ putnumber Then notahome =​ 1

          Next k

          If notahome =​ 1 Then

            notahomecount =​ notahomecount +​ 1

          Else

            home =​ j

          End If

        Next j

        If notahomecount =​ 8 Then

          sbox(i, home) =​ putnumber

          Call updatecantbelist(putnumber, i, home)

          Cells(i, home) =​ sbox(i, home)

        End If

      End If

    Next i

   

‘ now check all rows column by column for all numbers 1-​9 (putnumber)

‘ if the number is invalid in 8 of the nine cells in the same column, then the number

‘ must belong to the ninth cell (empty, of course)

For j =​ 1 To 9

    numberfound =​ 0